/*
 * Copyright 2010 Spolecne s.r.o. (www.spoledge.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.api.users;

import java.io.Serializable;

/**
 * This is a GWT emulation class.
 * It is ONLY used on the client side - JavaScript.
 *
 * This class is intended to use with custom DTO serializers
 * generated by AuDAO.
 *
 * Method hashCode() may differ from the original one.
 */
public class User implements Serializable, Comparable<User> {

    public static final long serialVersionUID = 8691571286358652288L;

    private String email;
    private String authDomain;
    private String userId;


    ////////////////////////////////////////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////////////////////////////////////////

    public User( String email, String authDomain ) {
        this( email, authDomain, null );
    }


    public User( String email, String authDomain, String userId ) {
        if (email == null) throw new IllegalArgumentException("email cannot be empty");
        if (authDomain == null) throw new IllegalArgumentException("authDomain cannot be empty");

        this.email = email;
        this.authDomain = authDomain;
        this.userId = userId;
    }


    @SuppressWarnings("unused")
    private User() {
    }


    ////////////////////////////////////////////////////////////////////////////
    // Public
    ////////////////////////////////////////////////////////////////////////////

    public String getEmail() {
        return email;
    }

    public String getAuthDomain() {
        return authDomain;
    }

    public String getUserId() {
        return userId;
    }


    public String getNickname() {
        int index = email.indexOf("@" + authDomain);

        if (index == -1) return email;
        else return email.substring( 0, index );
    }


    ////////////////////////////////////////////////////////////////////////////
    // Comparable
    ////////////////////////////////////////////////////////////////////////////

    public int compareTo( User o ) {
        return email.compareTo( o.email );
    }


    ////////////////////////////////////////////////////////////////////////////
    // Misc
    ////////////////////////////////////////////////////////////////////////////

    public boolean equals( Object o ) {
        if (this == o) return true;
        if (o == null || (!(o instanceof User))) return false;

        User u = (User) o;

        return email.equals( u.email ) && authDomain.equals( u.authDomain );
    }


    public int hashCode() {
        return email.hashCode() * 29 + authDomain.hashCode();
    }


    public String toString() {
        return email;
    }
}
